﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace wpKorabliksColleagues_WinForms_Test
{
    public partial class Form1 : Form
    {
        List<dsData> dsProperty = new List<dsData>();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            GetUsersList();
        }

        public void GetUsersList()
        {
            string sURL = "http://localhost";
            dsProperty =  new List<dsData>();

            using (SPSite site = new SPSite(sURL))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPContext cnt = SPContext.GetContext(web);
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    UserProfileManager uprofileManager = new UserProfileManager(context);
                    // UserProfileManager uprofileManager = GetProfiles(sURL);

                    IEnumerator profileEnumerator = uprofileManager.GetEnumerator();

                    dsProperty.Sort((x, y) => -x.HireDate.CompareTo(y.HireDate)); //    DateTime.Compare(x.Birthday, y.Birthday));

                    foreach (dsData row in dsProperty)
                    {
                    }

                    gv.DataSource = dsProperty;
                }
            }
        }

        //public static UserProfileManager GetProfiles(string strUrl)
        //{
        //    TopologyManager topology = new TopologyManager();
        //    PortalSite portal = topology.PortalSites[new Uri(strUrl)];
        //    Microsoft.SharePoint.Portal.PortalContext context = PortalApplication.GetContext(portal);
        //    UserProfileManager profileManager = new UserProfileManager(context);
        //    return profileManager;
        //}

    }


    class dsData
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Department { get; set; }
        public string Office { get; set; }
        public string ImageUrl { get; set; }
        public string SiteUrl { get; set; }
        public string Phone { get; set; }
        public string Mail { get; set; }
        public string Staff { get; set; }
        public DateTime Birthday { get; set; }
        public DateTime HireDate { get; set; }
        public string FullName { get; set; }
    }

}
